package com.hd.assistant.web.common.base;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.BindException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hd.assistant.web.common.util.ChangeCharset;

public class BasePageAnalysis {
	public  String getContent(String url,String charset) throws BindException {
		InputStream in = null;
		String s = "";
		HttpURLConnection conn = null;
		try {
			conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setRequestProperty("Connection", "close");
			in = conn.getInputStream();
			// 开始读取内容正文。
			BufferedReader br = new BufferedReader(new InputStreamReader(in,charset));
			StringBuffer sb = new StringBuffer();
			char[] charBuf = new char[1024 * 4];
			int len = br.read(charBuf);
			while (len != -1) {
				sb.append(charBuf, 0, len);
				len = br.read(charBuf);
			}
			br.close();
			s = sb.toString();
		} catch (BindException e) {
			throw e;
		} catch (Exception e) {
			// 其他异常暂不做处理 直接返回空
			return s;
		} finally {
			if (conn != null)
				conn.disconnect();
		}
		return s;
	}
	
	public static void main(String[] args) {
		BasePageAnalysis b = new BasePageAnalysis();
		long s = System.currentTimeMillis();
		String url = "http://fuwu.paipai.com/appstore/ui/my/app/appdetail/records.xhtml?appId=233891&pageNumber=";
		int count = 0;
		for(int i =10;i<5000;i++){
			StringBuffer sb = new StringBuffer();
			
			try {
				String str = b.getContent(url+i, "utf-8");
				Pattern p = Pattern.compile("userUin\":([0-9]*)");
				Matcher m = p.matcher(str);
				while(m.find()){
					count++;
					String ss = m.group(1);
					sb.append(ss).append("@qq.com;");
					if(count == 100){
						count = 0;
						System.out.println(sb.toString());
						sb = new StringBuffer();
					}
				}
				
			} catch (BindException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		long e = System.currentTimeMillis();
		System.out.println(e-s);	}
	
	
}
